var assert     = require('assert'),
    utility    = require('system/utility'),
	Dictionary = require('collections/dictionary'),
    LinqTests  = require('./linq.Tests');

DictionaryTests = (function () {
	var DictionaryTests = function() {
		var aComparer = function (x) { return x.a }
		var obj1 = { a: 1 }
		var obj1_ = { a: 1 }
		var obj2 = { a: 2 }
		var obj2_ = { a: 2 }

		var dataSets = [
			{
				enumerable : new Dictionary(),
				result : []
			}
		];

		var dictionary = new Dictionary();
		var result     = [];

		dictionary.add(1, 'a');
		
		result.push(dictionary.first());

		dataSets.push({ enumerable : dictionary, result : result });


		var tests = {};

		utility.extend(
			true,
			tests,
			LinqTests(dataSets),
			{
				'AddGetCountRemoveClear': function() {
					var dict = new Dictionary();
					dict.add('a', 1);
					dict.add('b', 2);
					dict.add('c', 3);
					dict.add('c', 100);
					assert.equal(1, dict.get('a'));
					assert.equal(2, dict.get('b'));
					assert.equal(100, dict.get('c'));

					dict.add(obj1, 1);
					dict.add(obj1_, 2);
					dict.add(obj2, 3);
					dict.add(obj2_, 4);
					assert.equal(1, dict.get(obj1));
					assert.equal(2, dict.get(obj1_));
					assert.equal(3, dict.get(obj2));
					assert.equal(4, dict.get(obj2_));

					assert.equal(7, dict.length);

					dict.remove('a');
					dict.remove(obj1);
					dict.remove(obj1_);
					dict.remove(obj2_);
					assert.equal(undefined, dict.get('a'));
					assert.equal(undefined, dict.get(obj1));
					assert.equal(undefined, dict.get(obj1_));
					assert.equal(undefined, dict.get(obj2_));

					assert.equal(3, dict.length);
					dict.clear();
					assert.equal(undefined, dict.get('b'));
					assert.equal(undefined, dict.get(obj2));
					assert.equal(0, dict.length);

					dict = new Dictionary(aComparer);

					dict.add(obj1, 1);
					dict.add(obj1_, 2);
					dict.add(obj2, 3);
					dict.add(obj2_, 4);
					assert.equal(2, dict.get(obj1));
					assert.equal(2, dict.get(obj1_));
					assert.equal(4, dict.get(obj2));
					assert.equal(4, dict.get(obj2_));

					assert.equal(2, dict.length);

					dict.remove(obj1);
					assert.equal(undefined, dict.get(obj1));
					assert.equal(undefined, dict.get(obj1_));

					assert.equal(1, dict.length);
					dict.clear();
					assert.equal(undefined, dict.get(obj2));
					assert.equal(undefined, dict.get(obj2_));
					assert.equal(0, dict.length);
				},
				'SetContains': function() {
					var dict = new Dictionary();
					dict.add('a', 1);
					dict.add('b', 2);
					dict.add(obj1, 1);
					dict.add(obj1_, 2);
					dict.set('a', 1000);
					dict.set('b', 2000);
					dict.set(obj1, 10000);
					dict.set(obj1_, 20000);
					assert.equal(1000, dict.get('a'));
					assert.equal(2000, dict.get('b'));
					assert.equal(10000, dict.get(obj1));
					assert.equal(20000, dict.get(obj1_));
					assert.ok(dict.contains('a'));
					assert.ok(dict.contains('b'));
					assert.ok(dict.contains(obj1));
					assert.ok(dict.contains(obj1_));
					assert.ok(!dict.contains('c'));
					assert.ok(!dict.contains(obj2));

					dict = new Dictionary(aComparer);
					dict.add(obj1, 1);
					dict.add(obj1_, 2);
					dict.add(obj2, 3);
					dict.add(obj2_, 4);
					dict.set(obj1, 10000);
					dict.set(obj1_, 20000);
					dict.set(obj2, 30000);
					dict.set(obj2_, 40000);
					assert.equal(20000, dict.get(obj1));
					assert.equal(20000, dict.get(obj1_));
					assert.equal(40000, dict.get(obj2));
					assert.equal(40000, dict.get(obj2_));
					assert.ok(dict.contains(obj1));
					assert.ok(dict.contains(obj1_));
					assert.ok(!dict.contains({ a: 3 }));
			},
				'toEnumerable': function() {
					var dict = new Dictionary();
					dict.add('a', 1);
					dict.add('b', 2);
					dict.add('c', 3);

					var ar = dict.orderBy('$.key').toArray();
					assert.equal('a', ar[0].key);
					assert.equal(1, ar[0].value);
					assert.equal('b', ar[1].key);
					assert.equal(2, ar[1].value);
					assert.equal('c', ar[2].key);
					assert.equal(3, ar[2].value);

					dict.clear();
					dict.add(obj1, 1);
					dict.add(obj1_, 2);
					dict.add(obj2, 3);
					dict.add(obj2_, 4);

					ar = dict.orderBy('$.key.a').toArray();
					assert.equal(obj1, ar[0].key);
					assert.equal(1, ar[0].value);
					assert.equal(obj1_, ar[1].key);
					assert.equal(2, ar[1].value);
					assert.equal(obj2, ar[2].key);
					assert.equal(3, ar[2].value);
					assert.equal(obj2_, ar[3].key);
					assert.equal(4, ar[3].value);

					dict = new Dictionary(aComparer);
					dict.add(obj1, 1);
					dict.add(obj1_, 2);
					dict.add(obj2, 3);
					dict.add(obj2_, 4);
					ar = dict.orderBy('$.key.a').toArray();
					assert.equal(obj1_, ar[0].key);
					assert.equal(2, ar[0].value);
					assert.equal(obj2_, ar[1].key);
					assert.equal(4, ar[1].value);
				}
			}
		);

		return tests;



		return {
		}
	}

	return DictionaryTests;
})();

// Hook into commonJS module systems
if (typeof module !== 'undefined' && "exports" in module) {
  module.exports = DictionaryTests;
}